package com.zhou.framework.config;

import com.zhou.framework.util.SettingUtil;
import com.zhou.org.common.LoginModeEnum;
import lombok.Data;
import org.apache.commons.collections.MapUtils;

import java.util.Map;

/**
 * @author lang.zhou
 * @since 2023/2/24 14:58
 */
@Data
public class PasswordPolicyConfig {
    private boolean enable;
    private boolean captchaEnable;
    private String captchaType;
    private int passwordRepeatCount ;
    private int sessionOut ;
    private int passwordStronger ;
    private String defaultPassword ;
    private int passwordMaxErrorCount ;
    private int passwordMaxErrorTime ;
    private int passwordMaxUse ;
    private int passwordOutRemindDay ;
    private int minLength ;

    private static PasswordPolicyConfig policy = new PasswordPolicyConfig();

    private static final Integer LOCK = 0;



    private PasswordPolicyConfig(){
        Map<String, Object> policyMap = SettingUtil.getSettingMap("password-policy");
        enable = MapUtils.getBoolean(policyMap,"enable",false);
        captchaEnable = MapUtils.getBoolean(policyMap,"captchaEnable",false);
        passwordRepeatCount = MapUtils.getIntValue(policyMap,"repeatCount",1);
        sessionOut = MapUtils.getIntValue(policyMap,"sessionOut",30);
        minLength = MapUtils.getIntValue(policyMap,"minLength",6);
        passwordMaxUse = MapUtils.getIntValue(policyMap,"passwordMaxUse",120);
        passwordOutRemindDay = MapUtils.getIntValue(policyMap,"passwordOutRemindDay",10);
        passwordStronger = MapUtils.getIntValue(policyMap,"passwordStronger",1);
        passwordMaxErrorCount = MapUtils.getIntValue(policyMap,"passwordMaxErrorCount",3);
        passwordMaxErrorTime = MapUtils.getIntValue(policyMap,"passwordMaxErrorTime",5);
        defaultPassword = MapUtils.getString(policyMap,"defaultPassword","111111");
        captchaType = MapUtils.getString(policyMap,"captchaType","math");
    }

    public static PasswordPolicyConfig getInstance(){
        return policy;
    }

    public static void refresh(){
        synchronized (LOCK){
            policy = new PasswordPolicyConfig();
        }
    }
}
